package com.javajun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JacksonInject.Value;
import com.javajun.custom.DataGridParam;
import com.javajun.custom.EasyuiUtils;
import com.javajun.custom.Success;
import com.javajun.entity.Book;
import com.javajun.entity.BookBorrow;
import com.javajun.entity.SysUser;
import com.javajun.entity.Teacher;
import com.javajun.form.BookBorrowForm;
import com.javajun.service.BookBorrowService;
import com.javajun.service.BookService;
import com.javajun.service.SysUserService;

@Controller
@RequestMapping(value="/bookBorrow")
public class BookBorrowContorller {

    @Autowired
    private BookBorrowService bookBorrowService;
    
    @Autowired
    private SysUserService sysUserService;
    
    @Autowired
    private BookService bookService;
    
    @RequestMapping(value="/borrowManage")
    public String borrowManageView(ModelMap map) {
        return "bookBorrow/borrowManage";
    }
    
    @RequestMapping(value="/manage")
    public String manageView() {
        return "bookBorrow/manage";
    }
    
    @RequestMapping(value="/search")
    public String searchView(BookBorrowForm form, ModelMap map) {
        map.put("form", form);
        return "bookBorrow/search";
    }
    
    @RequestMapping(value="/audit")
    public String auditView() {
        return "bookBorrow/audit";
    }
    
    @RequestMapping(value="/borrowAudit")
    public String borrowAuditView() {
        return "bookBorrow/borrowAudit";
    }
    
    @RequestMapping(value="/have")
    public String have() {
        return "bookBorrow/have";
    }
    
    @RequestMapping(value="/borrowHave")
    public String borrowHave() {
        return "bookBorrow/borrowHave";
    }
    
    @RequestMapping(value="borrowAudit.do")
    @ResponseBody
    public Object borrowAuditDo(Integer id) {
        Success success;
        String username = bookBorrowService.getCurrentUser().getUsername();
        SysUser sysUser = sysUserService.findByUsername(username);
        Book book = bookService.findById(id);
        BookBorrow br = new BookBorrow();
        br.setBook(book);
        br.setSysUser(sysUser);
        br.setStartTime(bookBorrowService.getNowTime());
        br.setState("1");
        try {
            bookBorrowService.save(br);
            book.setLend(book.getLend()+1);
            bookService.save(book);
            success = new Success(true, "申请成功，等待审核！");
        } catch (Exception e) {
            success = new Success(true, "哎哟，出现了未知的错误。");
        }
        return success;
    }
    
    @RequestMapping(value="/ok")
    @ResponseBody
    public Object okDo(Integer id) {
        Success success;
        BookBorrow entity = bookBorrowService.findById(id);
        entity.setState("2");
        entity.setMiddleTime(bookBorrowService.getNowTime());
        try {
            bookBorrowService.save(entity);
            success = new Success(true, "已同意该借书申请");
        } catch (Exception e) {
            success = new Success(false, "出现了未知的错误，被小怪兽吃了");
        }
        return success;
    }
    
    @RequestMapping(value="/end")
    @ResponseBody
    public Object endDo(Integer id) {
        Success success;
        BookBorrow entity = bookBorrowService.findById(id);
        Book book = entity.getBook();
        book.setLend(book.getLend() - 1 >= 0 ? book.getLend() - 1 : 0);
        entity.setState("3");
        entity.setFinishTime(bookBorrowService.getNowTime());
        try {
            bookBorrowService.save(entity);
            bookService.save(book);
            success = new Success(true, "还书成功！");
            
        } catch (Exception e) {
            success = new Success(false, "出现不可预料的错误，还书失败");
        }
        return success;
    }
    
    @RequestMapping(value="/delete")
    @ResponseBody
    public Object deleteDo(Integer id) {
        Success success;
        try {
            bookBorrowService.deleteById(id);
            success = new Success(true, "您已成功取消该申请");
        } catch (Exception e) {
            success = new Success(false, "出现了不可预料的错误~~~可能是被小怪兽吃掉了~");
        }
        return success;
    }
    
    @RequestMapping(value="/list.do", method=RequestMethod.POST)
    @ResponseBody
    public Object listDo(DataGridParam param, BookBorrowForm form) {
        Pageable pageable = param.buildPageable();
        Specification<BookBorrow> spec = bookBorrowService.buildSpec(form);
        Page<BookBorrow> page = bookBorrowService.findAll(spec, pageable);
        return EasyuiUtils.buildDatagridResult(page);
    }
    
    @RequestMapping(value="/list2.do", method=RequestMethod.POST)
    @ResponseBody
    public Object list2Do(DataGridParam param, BookBorrowForm form) {
        Pageable pageable = param.buildPageable();
        String username = bookBorrowService.getCurrentUser().getUsername();
        System.out.println("当前登录用户："+username);
        form.setUsername(username);
        Specification<BookBorrow> spec = bookBorrowService.buildSpec(form);
        Page<BookBorrow> page = bookBorrowService.findAll(spec, pageable);
        return EasyuiUtils.buildDatagridResult(page);
    }
    
}
